package com.pl.myshop.business.configure;

import com.pl.myshop.business.service.UserDetailsServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;

/**
 * @Auther: pl
 * @Date: 2020/3/22 20:30
 * @Description:服务安全配置，此处配置了认证模式，并在内存中配置了一个用户。
 */
@Configuration
@EnableWebSecurity
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    /**
     * @MethodName: passwordEncoder
     * @Description: 密码加密bean
     * @Param: []
     * @Return: org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
     * @Author: pl
     * @Date: 21:14
    **/
    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        // 配置默认的加密方式
        return new BCryptPasswordEncoder();
    }

    /**
     * @MethodName: userDetailsService
     * @Description: 这个bean用于为内存提供初始化用户
     * @Param: []
     * @Return: org.springframework.security.core.userdetails.UserDetailsService
     * @Author: pl
     * @Date: 21:05
    **/
    @Bean
    @Override
    protected UserDetailsService userDetailsService() {
        return new UserDetailsServiceImpl();
    }

    /**
     * @MethodName: configure
     * @Description: configure方法重写，用来增强。此处用于内存中初始化用户
     * @Param: [auth]
     * @Return: void
     * @Author: pl
     * @Date: 21:07
    **/
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService());
    }

    /**
     * 用于支持 password 模式 ,ouath2有四种模式，本项目采用密码模式。
     *
     * @return
     * @throws Exception
     */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    /**
     * @MethodName: configure
     * @Description: 忽略拦截/user/login
     * @Param: [web]
     * @Return: void
     * @Author: pl
     * @Date: 23:16
    **/
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers("/user/login");
    }

    /**
     * @MethodName: configure
     * @Description: 授权
     * @Param: [http]
     * @Return: void
     * @Author: pl
     * @Date: 0:03
    **/
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling()
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                 .and()
                .authorizeRequests()
               // 增加了授权访问配置
                .antMatchers("/user/info").hasAuthority("admin")
                .antMatchers("/user/logout").hasAuthority("admin")
                .antMatchers("/user/getuserinfo").hasAuthority("admin");

       /* http.exceptionHandling()
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // 增加了授权访问配置
                .antMatchers("/user/info").hasAuthority("ADMIN")
                .antMatchers("/user/logout").hasAuthority("ADMIN");*/
    }
}